<?php
namespace app\finance\admin;
use app\system\admin\Admin;
use app\product\model\Supplier as SupplierModel;
use app\finance\model\Account as AccountModel;
use app\finance\model\SystemBalanceLog as SystemBalanceLogModel;
use think\Db;

class SystemBalance extends Admin
{
    protected function initialize()
    {
        parent::initialize();
            $accounts = AccountModel::getSelect(AccountModel::where('id','<>',0)->where('status',1)->all());
            $this->assign('accounts', $accounts);
    }
	public function index()
	{
        // $list = Db::name('system_balance_log')->alias('sbl')
        // ->join('aftermarket_product s','s.sn=sbl.sn')
        // ->field('sbl.id,s.shop_id')
        // ->where('sbl.type',11)
        // ->select();
        // foreach ($list as $key => $value) {
        //     Db::name('system_balance_log')->where('id',$value['id'])->update(['shop_id'=>$value['shop_id']]);
        // }
        //
        // halt($list);

		if($this->request->isAjax()){




			$where      = $data = [];
            $page       = $this->request->param('page/d', 1);
            $limit      = $this->request->param('limit/d', 15);
            $account_id = $this->request->param('account_id/d', '');
            $cate       = $this->request->param('cate/d', '');
            $type       = $this->request->param('type', 1);
            $user_id       = $this->request->param('user_id', '');
            $shop_id       = $this->request->param('shop_id', '');
            $start      = $this->request->param('start', '2000-01-01');
            $end      = $this->request->param('end', date('Y-m-d H:i:s',time()));
            $where['is_effective'] = 1;
            if(!is_empty($account_id)){
                $where['account_id'] = $account_id;
            }
            if(!is_empty($user_id)){
                $where['user_id'] = $user_id;
            }
            if(!is_empty($shop_id)){
                $where['shop_id'] = $shop_id;
            }
            if(!is_empty($cate)){
                $where['cate'] = $cate;
            }
            if(empty($start)){
                $start = '2000-01-01';
            }
            if(empty($end)){
                $end = date('Y-m-d H:i:s',time());
            }
            $where['is_effective'] = 1;
            if($type == 2){
                $data['data'] = SystemBalanceLogModel::where($where)
                ->field('*,type as type_text,account_id as account_name')
                ->where($where)
                ->whereTime('createtime', [strtotime($start), strtotime($end)])
                // ->page($page)->limit($limit)
                ->order('id desc')->select();
                $data['count'] = SystemBalanceLogModel::where($where)->whereTime('createtime', [strtotime($start), strtotime($end)])->count();
                $data['code']   = 0;
                foreach ($data['data'] as $key => $value) {
                    if($value['cate'] == 1){
                        $data['data'][$key]['cate_text'] = '入账';
                    }else{
                        $data['data'][$key]['cate_text'] = '出账';
                    }
                    $data['data'][$key]['shop_name'] = Db::name('shop')->where('id',$value['shop_id'])->value('name');
                }
                return json($data);
            }else{
                $data['data'] = SystemBalanceLogModel::where($where)
                ->field('*,type as type_text,account_id as account_name,user_id as user_name')
                ->where($where)
                ->whereTime('createtime', [strtotime($start), strtotime($end)])
                ->page($page)->limit($limit)->order('id desc')->select();
                foreach ($data['data'] as $key => $value) {
                    $data['data'][$key]['shop_name'] = Db::name('shop')->where('id',$value['shop_id'])->value('name');
                }
                $data['count'] = SystemBalanceLogModel::where($where)->whereTime('createtime', [strtotime($start), strtotime($end)])->count();
                $data['code']   = 0;
                return json($data);
            }

        }
        $users = Db::name('user')->field('id,nick')->select();
        $this->assign('users',$users);
        $shops = Db::name('shop')->field('id,name')->select();
        $this->assign('shops',$shops);
		return $this->fetch();
	}
}
